package payrechargelogic

import (
	"context"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/dto"
	model "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/recharge"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SumUsersAmountInTimeDurationLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSumUsersAmountInTimeDurationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SumUsersAmountInTimeDurationLogic {
	return &SumUsersAmountInTimeDurationLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 查询时间段内的用户总金额
func (l *SumUsersAmountInTimeDurationLogic) SumUsersAmountInTimeDuration(in *rpc.SumUsersAmountInTimeDurationRequest) (*rpc.SumUsersAmountInTimeDurationResponse, error) {
	resp, err := model.NewRechargeOrderModel(l.ctx, l.svcCtx.Db).SumUsersAmountInTimeDuration(dto.SumUsersAmountInTimeDurationDto{
		UserIds: in.UserId,
		Start:   in.Start,
		End:     in.End,
	})
	if err != nil {
		return nil, err
	}
	var Data []*rpc.Data
	for _, v := range resp.Data {
		Data = append(Data, &rpc.Data{
			UserId:         v.UserId,
			RechargeAmount: v.TotalAmount,
		})
	}

	return &rpc.SumUsersAmountInTimeDurationResponse{
		Data: Data,
	}, nil
}
